package it.koutifaris.travianmap.dal.sqlservices;

import it.koutifaris.travianmap.context.TravianMapContext;
import it.koutifaris.travianmap.dal.MapServiceFactory;
import it.koutifaris.travianmap.dal.MapUIService;
import it.koutifaris.travianmap.dal.sqlservices.mapuiservice.SqlMapUIService;
import it.koutifaris.travianmap.dal.sqlservices.revision.SqlRevisionService;
import it.koutifaris.travianmap.dal.sqlservices.tile.SqlTileMapService;
import android.database.sqlite.SQLiteDatabase;

public class SqlMapServiceFactory implements MapServiceFactory {

	private final TravianMapContext context;
	private final String serverId;

	public SqlMapServiceFactory(TravianMapContext context, String serverId) {
		this.context = context;
		this.serverId = serverId;
	}

	@Override
	public SqlTileMapService mapService() {
		return new SqlTileMapService(serverId, newReadableDB());
	}

	@Override
	public SqlRevisionService revisionService() {
		return new SqlRevisionService(newReadableDB());
	}

	@Override
	public MapUIService mapUIService() {
		return new SqlMapUIService(newWritableDB());
	}

	private SQLiteDatabase newReadableDB() {
		return context.getSqlMapDbHelper(serverId).getReadableDatabase();
	}

	private SQLiteDatabase newWritableDB() {
		return context.getSqlMapDbHelper(serverId).getWritableDatabase();
	}

}
